<template>
  <div class="app-container">
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="150px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="订单号">
              <el-input v-model="form.order_sn" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="挂售人姓名/手机号">
              <el-input v-model="form.username" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="挂售卡/券">
              <el-input v-model="form.name" placeholder="输入卡券名称" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="8">
          <el-form-item label="挂售时间">
            <el-date-picker
              v-model="time"
              type="daterange"
              value-format="timestamp"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
          <Exprotcomponent :params="form" url="admin/sale_order/export" type="php" />
        </el-col>
      </el-form>
    </div>
    <div class="listDiv">
      <!-- {{$store.state.user.permissionJson}} -->
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          fixed="left"
          align="center"
          :index="table_index"
          width="50"
        />
        <el-table-column prop="order_sn" label="订单编号" />
        <el-table-column prop="sale_realname" label="挂售人姓名" />
        <el-table-column prop="sale_username" label="挂售人手机号">
          <template slot-scope="scope">
            <div>{{scope.row.sale_username | phonereg}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="card_name" label="挂售卡/券" />
        <el-table-column prop="sale_status" label="订单状态" />
        <el-table-column prop="price" label="原价" />
        <el-table-column prop="sale_price" label="挂售价" />
        <el-table-column prop="put_start_time" label="挂售时间">
          <template
            slot-scope="scope"
            v-if="scope.row.put_start_time"
          >{{ scope.row.put_start_time*1000 | date(1) }}</template>
        </el-table-column>
        <el-table-column prop="buy_realname" label="买家姓名" />
        <el-table-column prop="buy_username" label="买家手机号">
          <template slot-scope="scope">
            <div>{{scope.row.buy_username | phonereg}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="下单时间">
          <template slot-scope="scope">{{ scope.row.created_at*1000 | date(1) }}</template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template v-if="scope.row.sale_status" slot-scope="scope">
            <div v-if="scope.row.sale_status.indexOf('客服介入')>-1">
              <!-- <el-button size="small" type="text" @click="status(scope.row)">联系买家</el-button>
              <el-button size="small" type="text" @click="status(scope.row)">联系卖家</el-button> -->
              <el-button size="small" type="text" @click="status(scope.row,1)">取消订单</el-button>
              <el-button size="small" type="text" @click="status(scope.row,2)">确认发卡</el-button>
              <el-button size="small" type="text" @click="status(scope.row,3)">确认退卡</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="form.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import filter from "@/assets/filter";
import { getList, cancel, give_out, refund } from "@/api/ordersales/index";
export default {
  name: "",
  data() {
    return {
      time: "",
      total: 0,
      tableData: [{}],
      form: { page: 1, pagesize: 10 }
    };
  },
  watch: {
    time(val) {
      this.form.start_time = val ? val[0] / 1000 : "";
      this.form.end_time = val ? val[1] / 1000 : "";
    }
  },
  created() {
    this.list();
  },
  // beforeRouteLeave(to, from, next) {
  //   // ...
  //   console.log(to, from);
  //   next();
  // },
  methods: {
    async status(datas, num) {
      let res;
      const obj = {
        id: datas.id
      };
      if (num == 1) {
        res = await cancel(obj);
      }
      if (num == 2) {
        res = await give_out(obj);
      }
      if (num == 3) {
        res = await refund(obj);
      }
      console.log(res);
      this.$message.warning(res.data.msg);
      if (res.data.code == 200) {
        this.list();
      }
    },
    async list() {
      const res = await getList(this.form);
      console.log(res);
      if (res.data.code == 200) {
        this.tableData = res.data.data;
        this.total = res.data.total;
      }
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.pagesize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.list();
    },
    searchFn() {
      this.form.page = 1;
      this.list();
    },
    table_index(index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1;
    }
  }
};
</script>
<style  scoped>
.listDiv /deep/  .el-button{
  display: block;
  margin: auto;
}

</style>